#include "miniipc.h"
#include <iostream>
#include <cstring>
#include "cjson/cJSON.h"
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>

#define MAX_GROUP 40
#define DIF_IP1 "224.1.1.1"
static int miniipc_init_flag=0;


typedef struct {
    char ip[64];
    int  prot;
    char hande[128]; //限定过滤的消息头
    int  sockfd;
}MiniIpc_Basic;

// 线程执行的函数
void* thread_function(void* arg) {
    MiniIpc_Basic *p =(MiniIpc_Basic*)(arg);
    MiniIpc_Msg msg={0};
    printf("thread_function\n");
    while (1){
        int n = recvfrom(p->sockfd, &msg,sizeof(MiniIpc_Msg), MSG_WAITALL, NULL, 0);
        if(n!=sizeof(MiniIpc_Msg)){
            usleep(1000);
        }
        printf("recv %s\n",msg);
        usleep(100);
    }
    return NULL;
}

/***/
int MiniIpc_addgroup(int grepID,char * msgtopic,void *(*callback)(void *arg)){
    MiniIpc_Basic *miniIpcBasic;
    miniIpcBasic = (MiniIpc_Basic *)malloc(sizeof(MiniIpc_Basic));
    if(miniIpcBasic==nullptr){
        printf("miniIpcBasic is null \n");
        return -1;

    }
    if (grepID<0||grepID>MAX_GROUP){
        printf("grepID is too max %d\n",grepID);
        return -1;
    }
    miniIpcBasic->prot = 1024+grepID;
    strncpy(miniIpcBasic->ip,DIF_IP1, strlen(DIF_IP1));
    if(msgtopic==nullptr){
        strncpy(miniIpcBasic->hande,"", strlen(""));
    }else{
        strncpy(miniIpcBasic->hande,msgtopic, strlen(msgtopic));
    }

    int sockfd;

    struct sockaddr_in local_addr;
    struct ip_mreq group;

    // 创建UDP socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&local_addr, 0, sizeof(local_addr));
    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = INADDR_ANY; // 监听所有接口
    local_addr.sin_port = htons(miniIpcBasic->prot);

    // 绑定到本地地址
    if (bind(sockfd, (const struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) {
        perror("bind failed");
        close(sockfd);
        return -1;
    }

    // 加入组播组
    group.imr_multiaddr.s_addr = inet_addr(miniIpcBasic->ip);
    group.imr_interface.s_addr = htonl(INADDR_ANY);
    if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
        perror("setsockopt for IP_ADD_MEMBERSHIP failed");
        close(sockfd);
        return -1;
    }

    pthread_t thread_id;
    int ret = pthread_create(&thread_id, NULL, thread_function, miniIpcBasic);
    if (ret != 0) {
        fprintf(stderr, "Error: pthread_create failed\n");
        return -1;
    }
    return 0;
}


int MiniIpc_groupsend(int grepID,char msghandle[128],char msgstr[512],char msgbin[128]){
    MiniIpc_Basic miniIpcBasic={0};
    MiniIpc_Msg miniIpcMsg={0};
    if(msghandle==nullptr){
        strncpy(miniIpcMsg.hande,"",strlen(""));
    }else{
        strncpy(miniIpcMsg.hande,msghandle,strlen(msghandle));
    }
    if(msgstr==nullptr){
        strncpy(miniIpcMsg.msg,"",strlen(""));
    }else{
        strncpy(miniIpcMsg.msg,msgstr,strlen(msgstr));
    }
    if(msgbin==nullptr){
        memset(miniIpcMsg.normal,0,128);
    }else{
        memcpy(miniIpcMsg.normal,msgbin,128);
    }
    return -1;
}